﻿using System;
using Cinemachine;
using UnityEngine;

namespace Cameras
{
    public class CameraSizeFitter : MonoBehaviour
    {
        [SerializeField] private CinemachineVirtualCamera[] virtualCameras;
        [SerializeField] private int targetScreenHorizontalSize = 21;
        
        private int _screenWidth;
        
        
        private void Start()
        {
            if (Screen.width == _screenWidth)
            {
                return;
            }

            _screenWidth = Screen.width;
            FitCameras();
        }

        private void FitCameras()
        {
            float size = Screen.height * targetScreenHorizontalSize / (2f * Screen.width);
            
            foreach (CinemachineVirtualCamera camera in virtualCameras)
            {
                camera.m_Lens.OrthographicSize = size;
            }
        }
    }
}